package com.edu.bhu.admin.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.edu.bhu.common.core.domain.entity.SysUser;
import com.edu.bhu.common.utils.DateUtils;
import com.edu.bhu.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.bhu.admin.mapper.PreDetectionMapper;
import com.edu.bhu.admin.domain.PreDetection;
import com.edu.bhu.admin.service.IPreDetectionService;
import com.edu.bhu.common.core.text.Convert;

/**
 * 核酸检测Service业务层处理
 * 
 * @author yutianyang
 * @date 2022-04-23
 */
@Service
public class PreDetectionServiceImpl implements IPreDetectionService 
{
    @Autowired
    private PreDetectionMapper preDetectionMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询核酸检测
     * 
     * @param detectionId 核酸检测主键
     * @return 核酸检测
     */
    @Override
    public PreDetection selectPreDetectionByDetectionId(Long detectionId)
    {
        return preDetectionMapper.selectPreDetectionByDetectionId(detectionId);
    }

    /**
     * 查询核酸检测列表
     * 
     * @param preDetection 核酸检测
     * @return 核酸检测
     */
    @Override
    public List<PreDetection> selectPreDetectionList(PreDetection preDetection)
    {
        List<PreDetection> list = preDetectionMapper.selectPreDetectionList(preDetection);
        list = list.stream().map(m ->{
            SysUser sysUser = sysUserMapper.selectUserById(m.getUserId());
            m.setSysUser(sysUser);
            return m;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 查询某用户在十四天内的阳性核酸检测记录数
     *
     * @param preDetection 核酸检测
     * @return 核酸检测集合
     */
    @Override
    public Integer selectDangerCount(PreDetection preDetection){
        Date date = DateUtils.getDatePlusDays(DateUtils.getNowDate(), -14L);
        preDetection.setRedate(date);
        return preDetectionMapper.selectDangerCount(preDetection);
    }

    /**
     * 新增核酸检测
     * 
     * @param preDetection 核酸检测
     * @return 结果
     */
    @Override
    public int insertPreDetection(PreDetection preDetection)
    {
        return preDetectionMapper.insertPreDetection(preDetection);
    }

    /**
     * 修改核酸检测
     * 
     * @param preDetection 核酸检测
     * @return 结果
     */
    @Override
    public int updatePreDetection(PreDetection preDetection)
    {
        return preDetectionMapper.updatePreDetection(preDetection);
    }

    /**
     * 批量删除核酸检测
     * 
     * @param detectionIds 需要删除的核酸检测主键
     * @return 结果
     */
    @Override
    public int deletePreDetectionByDetectionIds(String detectionIds)
    {
        return preDetectionMapper.deletePreDetectionByDetectionIds(Convert.toStrArray(detectionIds));
    }

    /**
     * 删除核酸检测信息
     * 
     * @param detectionId 核酸检测主键
     * @return 结果
     */
    @Override
    public int deletePreDetectionByDetectionId(Long detectionId)
    {
        return preDetectionMapper.deletePreDetectionByDetectionId(detectionId);
    }
}
